package com.yinsin.jpabatis.type;

import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ArrayTypeHandler extends BaseTypeHandler<Object> {
	
	public ArrayTypeHandler() {
	    super();
	  }

	  @Override
	  public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
	    ps.setArray(i, (Array) parameter);
	  }

	  @Override
	  public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
	    return extractArray(rs.getArray(columnName));
	  }

	  @Override
	  public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	    return extractArray(rs.getArray(columnIndex));
	  }

	  @Override
	  public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	    return extractArray(cs.getArray(columnIndex));
	  }

	  protected Object extractArray(Array array) throws SQLException {
	    if (array == null) {
	      return null;
	    }
	    Object result = array.getArray();
	    array.free();
	    return result;
	  }
}
